package gui;

import javax.swing.*;
import javax.swing.table.*;
import database.EstateOperations;
import database.ProductOperations;
import model.EstateCollection;
import model.Product;
import model.ProductCollection;
import java.awt.*;
import java.awt.event.*;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;

@SuppressWarnings("serial")
public class productInputGUI extends JFrame implements ActionListener,ItemListener
{
	private JPanel p1, p2, p3,p4, p5, p15;
	private JLabel l1, l2, l3, l4, l5, l15;
	private JButton back, next, add ,remove;
	private JTextField f1, f3,f4;
	private ProductCollection pcoll;
	private JComboBox<String> f2;
	private String cat="Milk";
	private ResultSet rs;
	private JTable table;
	private DefaultTableModel model;
	private ProductOperations po;
	private Vector<Object> row;
	
	public productInputGUI(ProductCollection pc,ProductOperations p)
	{
		super("Product Input Screen");
		this.setSize(840, 500);
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		this.setLocationRelativeTo(null);
		this.setResizable(false);
		this.setLayout(new FlowLayout());
		this.pcoll=pc;
		this.po=p;
		po.dropProductTable();
		po.createProductTable();
		
		//Panel 1 
		p1 = new JPanel();
		p1.setPreferredSize(new Dimension(800, 40));
		l1 = new JLabel("Enter all your products here");
		p1.add(l1);
		
		//Panel 1.5 
		p15 = new JPanel();
		p15.setPreferredSize(new Dimension(800, 40));
		l15 = new JLabel("Products");
		l15.setFont(new Font("serif",Font.BOLD,26));
		p15.add(l15);
		
		//Panel 2
		p2 = new JPanel();
		p2.setLayout(new GridLayout(2,4));
		p2.setPreferredSize(new Dimension(500, 40));
		
		l2 = new JLabel("Name: ");
		l3 = new JLabel("Category: ");
		l4 = new JLabel("Supplier: ");
		l5 = new JLabel("Price: ");
		
		f1 = new JTextField(25);
		String[] options={"Milk", "Cream", "Eggs", "Butter", "Other"};
		f2 = new JComboBox<String>(options);
		f2.addItemListener(this);
		f3 = new JTextField(25);
		f4 = new JTextField(25);
		
		p2.add(l2);
		p2.add(f1);
		p2.add(l3);
		p2.add(f2);
		p2.add(l4);
		p2.add(f3);
		p2.add(l5);
		p2.add(f4);
		
		//Panel 3
		p3 = new JPanel();
		p3.setPreferredSize(new Dimension(800, 40));
		
		add = new JButton("ADD");
		add.addActionListener(this);
		remove = new JButton("REMOVE");
		remove.addActionListener(this);
		p3.add(add);
		p3.add(Box.createRigidArea(new Dimension(200,0)));
		p3.add(remove);
		
		//Panel 5
		p5 = new JPanel();
		p5.setPreferredSize(new Dimension(800, 35));
		
		back = new JButton("BACK");
		back.addActionListener(this);
		next = new JButton("NEXT");
		next.addActionListener(this);
		p5.add(back);
		p5.add(Box.createRigidArea(new Dimension(650,0)));
		p5.add(next);
		
		
		//Panel 4
		p4 = new JPanel();
		p4.setPreferredSize(new Dimension(800, 200));
		
		// create blank JTable on screen
		String[] cols={"ID","NAME","SUPPLIER","CATEGORY","PRICE"};
		int numRows=100;
		model=new DefaultTableModel(numRows,cols.length);
		model.setColumnIdentifiers(cols);
		table = new JTable(model);
		table.setFillsViewportHeight(true);
		table.setAutoCreateColumnsFromModel(false);
		JScrollPane sp=new JScrollPane(table);
		sp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
		sp.setPreferredSize(new Dimension(700, 190));
		//adjust size of columns on table
		TableColumnModel tbm=table.getColumnModel();
		tbm.getColumn(0).setMaxWidth(20);
		tbm.getColumn(3).setMaxWidth(150);
		tbm.getColumn(4).setMaxWidth(105);
		model.setRowCount(0);
		p4.add(sp,BorderLayout.CENTER);
		
	//Adding Panels to the page
	this.add(p1, BorderLayout.PAGE_START);
	this.add(p15, BorderLayout.NORTH);
	this.add(p2, BorderLayout.NORTH);	
	this.add(p3, BorderLayout.CENTER);
	this.add(p4, BorderLayout.SOUTH);
	this.add(p5, BorderLayout.PAGE_END);
	}
	
	
	public static void main(String args[]) 
	{
		ProductOperations po=new ProductOperations();
		ProductCollection p = new ProductCollection(po);
		productInputGUI pi = new productInputGUI(p,po);
		pi.setVisible(true);
	}
	
	//add product to database and array
	public void addProduct()
	{
		try
		{
			if(f1.getText().equals("")||f3.getText().equals(""))
			{
				JOptionPane.showMessageDialog(null, "You must enter data in the name and suppier fields","ERROR",JOptionPane.WARNING_MESSAGE);
			}
			else
			{
				String name=f1.getText();
				String supp=f3.getText();
				double price=Double.parseDouble(f4.getText());
				Product p = new Product(name,supp,cat,price);
				po.addProduct(p);
				pcoll.addProduct();
				f1.setText("");
				f3.setText("");
				f4.setText("");
			}
		}
		catch(NumberFormatException e)
		{
			JOptionPane.showMessageDialog(null, "You have not entered a correct price","ERROR",JOptionPane.WARNING_MESSAGE);
		}
	}
	
	//remove product from database and array
	public void removeProduct()
	{
		String a;
		a=JOptionPane.showInputDialog(null, "Enter the ID of the product you wish to remove","Remove Product",JOptionPane.QUESTION_MESSAGE);
		if(a==null)
		{
			//do nothing
		}
		else
		{
			int b=Integer.parseInt(a);
			boolean ans=pcoll.removeProduct(b);
			if(ans==true)
			{
				JOptionPane.showMessageDialog(null, "The product has ben deleted","Deleted",JOptionPane.WARNING_MESSAGE);
			}
			else
			{
				JOptionPane.showMessageDialog(null, "The product you choose is not a valid product","Not Deleted",JOptionPane.WARNING_MESSAGE);
			}
		}
	}
	
	// update the table after every new product added
	public void updateTable()
	{
		rs=po.getProduct();
		int counter=0;
		model.setRowCount(0);
		try {
			while(rs.next())
			{
				row=new Vector<Object>();
				row.add(rs.getInt(1));
				row.add(rs.getString(2));
				row.add(rs.getString(3));
				row.add(rs.getString(4));
				row.add(rs.getDouble(5));
				model.insertRow(counter,row);
				counter++;
			}
		} catch (SQLException e) 
		{
			e.printStackTrace();
		}
		
	}


	@Override
	public void actionPerformed(ActionEvent ae) 
	{
		if(ae.getSource()==add)
		{
			addProduct();
			updateTable();
		}
		if(ae.getSource()==remove)
		{
			removeProduct();
			updateTable();
		}
		if(ae.getSource()==next)
		{
			try
			{
				po.closeDB();
			}
			catch(NullPointerException e)
			{
				po.closeDB2();
			}
			EstateOperations eo=new EstateOperations();
			EstateCollection ec=new EstateCollection(eo);
			deliveryInputGUI dig=new deliveryInputGUI(eo,ec);
			dig.setVisible(true);
			this.setVisible(false);
		}
		if(ae.getSource()==back)
		{
			po.closeDB2();
			securityGUI sg=new securityGUI();
			sg.setVisible(true);
			this.setVisible(false);
		}
	}

	@Override
	public void itemStateChanged(ItemEvent ae) 
	{
		if (ae.getSource() == f2 && ae.getStateChange() == ItemEvent.SELECTED)
		{
			cat=(String)f2.getSelectedItem();
		}
		
	}

}
